package com.yang.mybatis.config.parser;

import com.yang.mybatis.config.MybatisConfiguration;
import com.yang.mybatis.config.MybatisDataSource;
import com.yang.mybatis.config.MybatisEnvironment;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class XmlMybatisConfigurationParser implements IMybatisConfigurationParser {

    @Override
    public MybatisConfiguration parser(String path) {
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            SAXReader saxReader = new SAXReader();
            // 1. 读取文档
            Document document = saxReader.read(inputStream);
            Element root = document.getRootElement();

            parseEnvironments(mybatisConfiguration, root.element("environments"));
            parseMappers(mybatisConfiguration, root.element("mappers"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mybatisConfiguration;
    }

    private void parseMappers(MybatisConfiguration mybatisConfiguration, Element mappers) {
        List<Element> mapperList = mappers.elements("mapper");
        for (Element mapper : mapperList) {
            String resource = mapper.attributeValue("resource");
            mybatisConfiguration.addMapperPath(resource);
        }
    }

    private void parseEnvironments(MybatisConfiguration mybatisConfiguration, Element environments) {
        String defaultId = environments.attributeValue("default");
        for (Element element : environments.elements()) {
            String id = element.attributeValue("id");

            Element dataSource = element.element("dataSource");
            List<Element> properties = dataSource.elements("property");
            Map<String, String> propertyMap = new HashMap<>();
            for (Element property : properties) {
                propertyMap.put(property.attributeValue("name"), property.attributeValue("value"));
            }
            String driver = propertyMap.get("driver");
            String url = propertyMap.get("url");
            String username = propertyMap.get("username");
            String password = propertyMap.get("password");
            MybatisDataSource mybatisDataSource = new MybatisDataSource();
            mybatisDataSource.setDriver(driver);
            mybatisDataSource.setUrl(url);
            mybatisDataSource.setUsername(username);
            mybatisDataSource.setPassword(password);

            MybatisEnvironment mybatisEnvironment = new MybatisEnvironment();
            mybatisEnvironment.setId(id);
            mybatisEnvironment.setMybatisDataSource(mybatisDataSource);
            mybatisConfiguration.addEnvironment(id, mybatisEnvironment);
            if (id.equals(defaultId)) {
                mybatisConfiguration.setDefaultMybatisEnvironment(mybatisEnvironment);
            }
        }
    }
}
